package com.zg.autoform.service.impl;

import com.zg.autoform.service.FormDivService;
import com.zg.common.core.dao.autoform.*;
import com.zg.common.core.service.PageBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;



import static com.zg.autoform.sys.AutoFormE.DIY_FORM_DIV_IS_EXIST;

/**
 * DESCRIPTION
 *
 * @author zhanfeng.liang
 * @since 2025-07-17
 */
@Service
@Slf4j
public class FormDIvServiceImpl extends PageBaseService<DiyFormDiv ,DiyFormDivDao> implements FormDivService {


//    @Autowired
//    @Qualifier("diyFormDivDao")
//    @Override
//    protected void setDao(IDao<DiyFormDiv> dao) {
//        this.dao = dao;
//    }
    @Override
    public  DiyFormDiv getByFormAndUserId(Long formId, Long userId){
        DiyFormDiv formDiv = new DiyFormDiv();
        formDiv.setFormId(formId);
        formDiv.setUserId(userId);
        List<DiyFormDiv> list = list(formDiv);
        if(!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

    @Override
    protected void preAdd(DiyFormDiv entity) {
        DiyFormDiv diyFormDiv = getByFormAndUserId(entity.getFormId(),entity.getUserId());
        if(diyFormDiv!=null){
            DIY_FORM_DIV_IS_EXIST.happen();
        }
    }
    @Override
    protected void preEdit(DiyFormDiv entity) {
        DiyFormDiv diyFormDiv = getByFormAndUserId(entity.getFormId(),entity.getUserId());
        if(diyFormDiv!=null&&!diyFormDiv.getId().equals(entity.getId())){
            DIY_FORM_DIV_IS_EXIST.happen();
        }
    }
}
